/*
 * xmlparser.c
 *
 *  Created on: 2010-10-1
 *      Author: jack
 */

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdlib.h>
#include <string.h>
#include "../include/structs.h"

static int mktempfolder(char *wgetcmd)
{
	if(system(wgetcmd)==-1)
	{
		fprintf(stderr,"Error!");
		return -1;
	}
	else
	{
		return 0;
	}
}

static songinfo *parse_songinfo()
{
	songinfo *head=(songinfo *)malloc(sizeof(songinfo));

	head->prev=NULL;

	songinfo *cursong=head;
	songinfo *prevsong=head;

    xmlDocPtr doc;           //定义解析文档指针

    xmlNodePtr curNode;      //定义结点指针(你需要它为了在各个结点间移动)

    xmlChar *szKey;          //临时字符串变量

    xmlNodePtr curchild;

    char *szDocName;



    szDocName = "/tmp/.songinfo.xml";

    doc = xmlRecoverFile(szDocName); //解析文件

    //检查解析文档是否成功，如果不成功，libxml将指一个注册的错误并停止。

    //一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。

    //如果文档是这样，libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中.

    if (NULL == doc)

    {

       fprintf(stderr,"Document not parsed successfully. \n");

       return NULL;

    }

    curNode = xmlDocGetRootElement(doc); //确定文档根元素

    /*检查确认当前文档中包含内容*/

    if (NULL == curNode)

    {

       fprintf(stderr,"empty document\n");

       xmlFreeDoc(doc);

       return NULL;

    }

    /*在这个例子中，我们需要确认文档是正确的类型。“diggcd”是在这个示例中使用文档的根类型。*/

    if (xmlStrcmp(curNode->name, BAD_CAST("diggcd")))

    {

       fprintf(stderr,"document of the wrong type, root node != 'diggcd'");

       xmlFreeDoc(doc);

       return NULL;

    }

    curNode = curNode->xmlChildrenNode;

    while(curNode != NULL)
    {
    	cursong->next=(songinfo *)malloc(sizeof(songinfo));
    	cursong->next->prev=cursong;/*Set Previous Song!!!!*/
    	cursong=cursong->next;
    	cursong->next=NULL; /*这句一定要！NULL比什么都好！！*/

       //取出节点中的内容
    	curchild=curNode->xmlChildrenNode;

       if ((!xmlStrcmp(curchild->name, (const xmlChar *)"title")))
       {
           szKey = xmlNodeGetContent(curchild);
           strcpy(cursong->title,szKey);
           xmlFree(szKey);
       }
       curchild=curchild->next;
       if ((!xmlStrcmp(curchild->name, (const xmlChar *)"artist")))
       {
           szKey = xmlNodeGetContent(curchild);
           strcpy(cursong->artist,szKey);
           xmlFree(szKey);
       }
       curchild=curchild->next;
       if ((!xmlStrcmp(curchild->name, (const xmlChar *)"album")))
       {
           szKey = xmlNodeGetContent(curchild);
           strcpy(cursong->album,szKey);
           xmlFree(szKey);
       }
       curchild=curchild->next;
       if ((!xmlStrcmp(curchild->name, (const xmlChar *)"url")))
       {
           szKey = xmlNodeGetContent(curchild);
           strcpy(cursong->url,szKey);
           xmlFree(szKey);
       }
       curchild=curchild->next;
       if ((!xmlStrcmp(curchild->name, (const xmlChar *)"id")))
       {
           szKey = xmlNodeGetContent(curchild);
           strcpy(cursong->id,szKey);
           xmlFree(szKey);
       }
       curNode=curNode->next;
    }

    xmlFreeDoc(doc);

    return head;
}

static searchresult * parse_search()
{
	searchresult *head=(searchresult *)malloc(sizeof(searchresult));

	searchresult *curresult=head;

    xmlDocPtr doc;           //定义解析文档指针

    xmlNodePtr curNode;      //定义结点指针(你需要它为了在各个结点间移动)

    char *szKey;          //临时字符串变量

    xmlNodePtr curchild;

    char *szDocName;



    szDocName = "/tmp/.searchresult.xml";

    doc = xmlRecoverFile(szDocName); //解析文件

    //检查解析文档是否成功，如果不成功，libxml将指一个注册的错误并停止。

    //一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。

    //如果文档是这样，libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中.

    if (NULL == doc)

    {

       fprintf(stderr,"Document not parsed successfully. \n");

       return NULL;

    }

    curNode = xmlDocGetRootElement(doc); //确定文档根元素

    /*检查确认当前文档中包含内容*/

    if (NULL == curNode)

    {

       fprintf(stderr,"empty document\n");

       xmlFreeDoc(doc);

       return NULL;

    }

    /*在这个例子中，我们需要确认文档是正确的类型。“searchresult”是在这个示例中使用文档的根类型。*/

    if (xmlStrcmp(curNode->name, BAD_CAST("searchresult")))

    {

       fprintf(stderr,"document of the wrong type, root node != 'searchresult'");

       xmlFreeDoc(doc);

       return NULL;

    }

    curNode = curNode->xmlChildrenNode;

    while(curNode != NULL)
    {
    	curresult->next=(searchresult *)malloc(sizeof(searchresult));
    	curresult=curresult->next;
    	curresult->next=NULL; /*这句一定要！NULL比什么都好！！*/

       //取出节点中的内容
    	curchild=curNode->xmlChildrenNode;

        if ((!xmlStrcmp(curchild->name, (const xmlChar *)"title")))
         {
             szKey = xmlNodeGetContent(curchild);
             strcpy(curresult->title,szKey);
             xmlFree(szKey);
         }
         curchild=curchild->next;

         if ((!xmlStrcmp(curchild->name, (const xmlChar *)"artist")))
          {
              szKey = xmlNodeGetContent(curchild);
              strcpy(curresult->artist,szKey);
              xmlFree(szKey);
          }
          curchild=curchild->next;
          if ((!xmlStrcmp(curchild->name, (const xmlChar *)"type")))
           {
               szKey = xmlNodeGetContent(curchild);
               strcpy(curresult->type,szKey);
               xmlFree(szKey);
           }
           curchild=curchild->next;
           if ((!xmlStrcmp(curchild->name, (const xmlChar *)"id")))
            {
                szKey = xmlNodeGetContent(curchild);
                strcpy(curresult->id,szKey);
                xmlFree(szKey);
            }
           curNode=curNode->next;
    }

    xmlFreeDoc(doc);

    return head;
}

static lyric *parse_lyric()
{
	lyric *head=(lyric *)malloc(sizeof(lyric));

	head->prev=NULL;

	lyric *curlyric=head;
	lyric *prevlyric=head;

    xmlDocPtr doc;           //定义解析文档指针

    xmlNodePtr curNode;      //定义结点指针(你需要它为了在各个结点间移动)

    xmlChar *szKey;          //临时字符串变量

    xmlNodePtr curchild;

    char *szDocName;



    szDocName = "/tmp/.lyric.xml";

    doc = xmlRecoverFile(szDocName); //解析文件

    //检查解析文档是否成功，如果不成功，libxml将指一个注册的错误并停止。

    //一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。

    //如果文档是这样，libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中.

    if (NULL == doc)

    {

       fprintf(stderr,"Document not parsed successfully. \n");

       return NULL;

    }

    curNode = xmlDocGetRootElement(doc); //确定文档根元素

    /*检查确认当前文档中包含内容*/

    if (NULL == curNode)

    {

       fprintf(stderr,"empty document\n");

       xmlFreeDoc(doc);

       return NULL;

    }

    /*在这个例子中，我们需要确认文档是正确的类型。“lyric_xml”是在这个示例中使用文档的根类型。*/

    if (xmlStrcmp(curNode->name, BAD_CAST("lyric_xml")))

    {

       fprintf(stderr,"document of the wrong type, root node != 'lyric_xml'");

       xmlFreeDoc(doc);

       return NULL;

    }

    curNode = curNode->xmlChildrenNode;

    while(curNode != NULL)
    {
    	curlyric->next=(lyric *)malloc(sizeof(lyric));
    	curlyric->next->prev=curlyric;/*Set Previous Song!!!!*/
    	curlyric=curlyric->next;
    	curlyric->next=NULL; /*这句一定要！NULL比什么都好！！*/

       //取出节点中的内容
    	curchild=curNode->xmlChildrenNode;

       if ((!xmlStrcmp(curchild->name, (const xmlChar *)"time")))
       {
           szKey = xmlNodeGetContent(curchild);
           strcpy(curlyric->time,szKey);
           xmlFree(szKey);
       }
       curchild=curchild->next;
       if ((!xmlStrcmp(curchild->name, (const xmlChar *)"lyric")))
       {
           szKey = xmlNodeGetContent(curchild);
           strcpy(curlyric->lyric,szKey);
           xmlFree(szKey);
       }


       curNode=curNode->next;
    }

    xmlFreeDoc(doc);

    return head;
}

searchresult *get_search_result(char *keywords)
{
	int i=0,count=strlen(keywords);
	while(i<count)//Replace SPACE with &
	{
		if(keywords[i]==' ')
		{
			keywords[i]='&';
		}
		else
		i++;
	}

	char *cmd=(char *)malloc((200+count)*sizeof(char));
	/*Remove orig file*/
	system("rm -f /tmp/.searchresult.xml");
	sprintf(cmd,"%s%s%c","wget -r -q -O /tmp/.searchresult.xml \"http://172.16.108.137/index.aspx?pw=Shit&action=search&keywords=",keywords,'\"');
	mktempfolder(cmd);  //Get XML file
	return parse_search();
}


songinfo *get_song_info(char *querystring)
{
	/*Remove orig file*/
	system("rm -f /tmp/.songinfo.xml");
	char *cmd=(char *)malloc(300*sizeof(char));
	sprintf(cmd,"%s%s%c","wget -r -q -O /tmp/.songinfo.xml \"http://172.16.108.137/index.aspx?",querystring,'\"');
	mktempfolder(cmd);  //Get XML file
	return parse_songinfo();   //Parse XML file and generate songinfo linked list
}


lyric *get_lyric(char *querystring)
{
	system("rm -f /tmp/.lyric.xml");
	char *cmd=(char *)malloc(300*sizeof(char));
	sprintf(cmd,"%s%s%c","wget -r -q -O /tmp/.lyric.xml \"http://172.16.108.137/index.aspx?",querystring,'\"');
	mktempfolder(cmd);  //Get XML file
	return parse_lyric();
}
